<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
	<title>药品库管理</title>
	<%@ include file="/WEB-INF/component/commonCSS.jsp" %> 
    <!--[if lt IE 9]>
        <script src="lib/js/html5shiv.min.js"></script>
        <script src="lib/js/respond.min.js"></script>
    <![endif]-->
</head>
<body class="skin-blue-light sidebar-mini fixed skin-blue-light-frame">
    <section class="content-header">
        <h1>药品库管理</h1>
        <ol class="breadcrumb">
            <li><a href="${ctx}/home.jsp"><i class="fa fa-home"></i>首页</a></li>
            <li class="active"><a href="drugList.jsp">药品库管理</a></li>
        </ol>
    </section>
    <section class="content">
        <div class=" box box-success">
            <div class="box-body">
                <div class=" form-inline">
                    <div class="form-group form-group-xs">
                     <label>药品编码：</label>
                        <input class="form-control" id="drugcode" name="drugcode" type="text" placeholder="药品编码" />
                    </div>
                     <div class="form-group form-group-xs">
                        <label>通用名：</label>
                        <input class="form-control" id="drugname" name="drugname" type="text" placeholder="通用名" />
                    </div>
                     
                    <div class="form-group form-group-xs">
                        <label>生产企业：</label>
                        <input type="hidden" id="prodPompId" name="prodPompId" value="">
                        <input class="form-control"  id="comp" name="comp" type="text" placeholder="生产企业" />
                 
                   </div>
                    <button type="button" onclick="gridSerch();" class="btn btn-success btn-sm"> 查&nbsp;询</button>
                </div>
            </div>
        </div>
        <table class="jqgrid" id="gridlist"></table>
        <div id="gridpage"></div>
        <div class=" box box-warning bottongroup">
            <a href="${ctx }/stdDrug/toAdd.html" class="btn btn-default btn-sm" >新&nbsp;增</a>
            <label  onclick="batchChangeStatus('1')" class="btn btn-default btn-sm">启&nbsp;用</label>
	        <label onclick="batchChangeStatus('0')" class="btn btn-default btn-sm">停&nbsp;用</label>
	        <label onclick="mergeDrugInfo();" class="btn btn-default btn-sm">合&nbsp;并</label>
        </div>
        
        <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h5 class="modal-title" id="exampleModalLabel">药品修改</h5>
                    </div>
                    <div class="modal-body">
                        <form>
                            <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label">药品编码：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtdrugCode" placeholder="药品 编码">
                        </div>
                    </div>
                    
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label">药理分类编码：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtdrugPharmCode" placeholder="药理分类编码">
                        </div>
                    </div>
                    
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class="control-label">药理分类名称：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtdrugPharmName" placeholder="药理分类编码">
                        </div>
                    </div>
                    
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label">通用名：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtCommonName" placeholder="通用名">
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label">剂型：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtdrug" placeholder="剂型">
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label">规格：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtnorms" placeholder="规格">
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label">单位：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtunit" placeholder="单位">
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label">转换系数：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtfactor" placeholder="转换系数">
                        </div>
                    </div>
                    <div class="form-group form-group-xs">
                        <label for="recipient-name" class=" control-label">生产企业：</label>
                        <div class="">
                            <input type="text" class="form-control" id="txtmanufacturer" placeholder="生产企业">
                        </div>
                    </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" onclick="save();" class="btn btn-primary btn-sm">保&nbsp;存</button>
                        <button type="button" class="btn btn-danger btn-sm" data-dismiss="modal">关&nbsp;闭</button>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="modal fade" id="selectSpec" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h5 class="modal-title" id="exampleModalLabel">选择规格</h5>
                    </div>
                    <div class="modal-body">
                        <form>
                        <div class=" box box-success">
            <div class="box-body">
            <div class="form-inline">
                    <div class="form-group form-group-xs">
                         规格名称： 
                        <input class="form-control" type="text" id="specName" placeholder="规格名称" />
                    </div>
                    
                    <button type="button" onclick="gridSpecSerch()" class="btn btn-success btn-sm">查询</button>
                </div>
            </div>
        </div>
                        
                        <div class="form-group form-group-xs">
                                <table class="jqgrid col-sm-1" id="gridlist2"></table>
        <div id="gridpage2 col-sm-1"></div>
                            </div>
                        
                        
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" onclick="gridSpecOk()" class="btn btn-primary btn-sm">选择</button>
                        <button type="button" class="btn btn-danger btn-sm" onclick="cleanSpecdata()">清空</button>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="modal fade" id="selectComp" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header bg-primary">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                        <h5 class="modal-title" id="exampleModalLabel">选择生产企业</h5>
                    </div>
                    <div class="modal-body">
                        <form>
                        <div class=" box box-success">
            <div class="box-body">
            <div class="form-inline">
                    <div class="form-group form-group-xs">
                         企业编码： 
                        <input class="form-control" type="text" id="compCode" placeholder="企业编码" />
                    </div>
                    <div class="form-group form-group-xs">
                         企业名称： 
                        <input class="form-control" type="text" id="compName" placeholder="企业名称" />
                    </div>
                    
                    <button type="button" onclick="gridSerch()" class="btn btn-success btn-sm">查询</button>
                </div>
            </div>
        </div>
                        
                        <div class="form-group form-group-xs">
                                <table class="jqgrid col-sm-1" id="gridlist1"></table>
        <div id="gridpage1 col-sm-1"></div>
                            </div>
                        
                        
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" onclick="gridOk()" class="btn btn-primary btn-sm">选择</button>
                        <button type="button" class="btn btn-danger btn-sm" onclick="cleandata()">清空</button>
                    </div>
                </div>
            </div>
        </div>
    </section>
    <%@ include file="/WEB-INF/component/commonJS.jsp" %> 
    <script type="text/javascript">
        $(function () {
        	
            $("#gridlist").jqGrid({
                url: "${pageContext.request.contextPath}/stdDrug/getData.html",
                mtype: "POST",
                datatype: "json",
                autowidth: true,
                height: 270,
                colNames: [ 'id','药品编码',  '通用名', '剂型', '规格', '转换系数', '单位','材质', '生产企业', '状态','操作'],
                colModel: [
					{ name: 'drugId', width: 120 ,hidden:true,key:true, align:"center",sortable: false},
                    { name: 'drugCode', width: 100 , align:"center",sortable: false},
                    { name: 'drugName', width: 140 , align:"left" ,sortable: false},
                    { name: 'formName', width: 80 , align:"center" ,sortable: false},
                    { name: 'specName', width: 80 , align:"left" ,sortable: false},
                    { name: 'factor', width: 80 , align:"center" ,sortable: false},
                    { name: 'unit', width: 40 , align:"center" ,sortable: false},
                    { name: 'materialName', width: 80 , align:"center" ,sortable: false},
                    { name: 'prodPompName', width: 180 , align:"left",sortable: false },
                    { name: 'status', width: 40 , align:"center",sortable: false },
                    { name: 'operate', width: 180, align: "center",sortable: false, formatter : function(val, opts, rowdata) {
                    	var str = "";
                    	if (rowdata.status == "启用"){
                    		str += "<a href='${pageContext.request.contextPath}/stdDrug/toEdit.html?drugId="+rowdata.drugId+"'>修改</a>";
                    		str += "<a href='javascript:void(0);' onclick=\"startOrStop('"+rowdata.drugId+"','0')\">停用</a>";
                    	} else if (rowdata.status == "停用"){
                    		str += "<a href='javascript:void(0);' onclick=\"startOrStop('"+rowdata.drugId+"','1')\">启用</a>";
                    	}
                    	str += "<a href='${pageContext.request.contextPath}/stdDrug/loglist.html?drugId="+rowdata.drugId+"'>维护记录</a>"
                    	+"<a href='${pageContext.request.contextPath}/stdDrug/drugInfoDetailPop.html?drugId="+rowdata.drugId+"'>详情</a>";
                    	return str;
                    }}
                   
                ],
                rowNum: 20,
                rowList: [10,20,50,100],
                rownumbers: true,
                pager: "#gridpage",
                multiselect: true, 
                viewrecords: true,
                caption: "药品库管理",
                jsonReader: {
                    repeatitems: false,
                    id: "ids"
                }
            });
            $("#gridlist").jqGrid('navGrid', '#gridpage', { add: false, edit: false, del: false, search: false, refresh: false });
            $(window).trigger("resize");
            moreNoMore();
            
            
            
            
            $("#drugform1").HNSelect({
                url: "${pageContext.request.contextPath}/selectController/getForm.html", data: { ID: '0' },  defaultText: "<option value=''>请选择</option>",
                func: function () {
                    $("#drugform2").HNSelect({
                        parent_selector: "#drugform1", url: "${pageContext.request.contextPath}/selectController/getForm.html", dataid: "ID",  defaultText: "<option value=''>请选择</option>",
                        
                    });
                }
            });
    		
            
        });
        
        
        
        
        
        
        
        
        
        function gridSerch() {
        	var drugcode = $("#drugcode").val();
        	var drugname = $("#drugname").val();
        	var drugform = $("#drugform2").val();
        	if (drugform.length <1){
        		drugform = $("#drugform1").val();
        	}
        	var specId = $("#specId").val();
        	var prodPompId = $("#prodPompId").val();
    		$("#gridlist").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{"drugCode":drugcode,
    	        		"drugName":drugname,
    	        		"formId":drugform,
    	        		"specId":specId,
    	        		"prodPompId":prodPompId}
    	    }).trigger("reloadGrid"); //重新载入  
    	}
        
        
        function startOrStop(id, status) {
        	var msg = "";
        	if (status == 1){
        		msg = "启用";
        	} else if (status == 0){
        		msg = "停用";
        	}
        	 $.HN.message.confirm('确定'+msg+'吗？', '', '').on(function (e) {
        		 if(e){
        			 $.post("${pageContext.request.contextPath}/stdDrug/drugStartOrStop.html", {
        	    			drugId : id,
        	    			status : status
        	    		}, function(result) {
        	    			
        	    			if (result == "success") {
        	    				gridSerch();
        	    				$.HN.message.alert("操作成功！", "消息", "success");
        	    			} else {
        	    				$.HN.message.alert("操作失败！", "消息", "error");
        	    			}
        	    		}, "json")
        		 }
        		 
        	 });
    	}
        
        
        
        
        
        function batchChangeStatus(status) {
        	
    		var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
    		if (rowIds.length <= 0) {
    			$.HN.message.alert("请选择要操作的行！", "消息", "info");
    			return;
    		}
    		
    		var msg = "";
        	if (status == 1){
        		msg = "启用";
        	} else if (status == 0){
        		msg = "停用";
        	}
    		$.HN.message.confirm('确定'+msg+'吗？', '', '').on(function (e) {
    			if (e) {
    				var list = [];
    	    		for (var i=0; i<rowIds.length; i++) {
    	    			rowId = rowIds[i];
    	    			list.push({"drugId":rowId, "status":status});
    	    		}
    	    		$.post("${pageContext.request.contextPath}/stdDrug/batchStartOrStop.html", {
    	    			"array" : json.jsonToString(list)
    	    		}, function(result) {
    	    			//var result = eval("("+result+")");
    	    			if (result == "success") {
    	    				gridSerch();
    	    				$.HN.message.alert("操作成功！", "消息", "success");
    	    			} else {
    	    				$.HN.message.alert("操作失败！", "消息", "error");
    	    			}
    	    		}, "json")
    			}
    		});
    		
    		
    		
    		
    	}
        
        
        
        
        
        
        
        
        
        
        
        function batch(status){
        	
        }
        
        
        function deleteId(){
      	  $.HN.message.confirm('确定停用吗？', '', '').on(function (e) {
                if (e) {
                    $.HN.message.alert('操作成功！', '提示信息', 'success');
                }
            });
        } 
        $('#allStart').click(function () {
      	  var checked = false;
        	 $("input.cbox").each(function() {
        	     if($(this).is(":checked")) {
        	    	 checked = true;
        	    	 return false;
        	     };
        	 });
        	 
        	 if (!checked) {
        		 $.HN.message.alert('请选择要启用的条目', '提示信息', 'failed');
        	 }else{
            $.HN.message.confirm('确定启用吗？', '', '').on(function (e) {
                if (e) {
                    $.HN.message.alert('操作成功！', '提示信息', 'success');
                }
            });
        	 }
      });
        $('#allDelete').click(function () {
        	var checked = false;
       	 $("input.cbox").each(function() {
       	     if($(this).is(":checked")) {
       	    	 checked = true;
       	    	 return false;
       	     };
       	 });
       	 
       	 if (!checked) {
       		 $.HN.message.alert('请选择要停用的条目', '提示信息', 'failed');
       	 }else{
                $.HN.message.confirm('确定停用吗？', '', '').on(function (e) {
                    if (e) {
                        $.HN.message.alert('操作成功！', '提示信息', 'success');
                    }
                });
       	 }
         });
        $('#combine').click(function () {
            window.location.href="drugMerge.jsp";
     });
        /*  function edit(){
  /*          $.HN.message.confirm('确定要修改吗？', '', '').on(function (e) {
                if (e) {
                    $.HN.message.alert('操作成功！', '提示信息', 'success');
                    window.location.href="drugEdit.jsp";
                    
                }
            });
  
        	window.location.href="drugEdit.jsp";
        }*/
        function edit(drugCode,drugPharmCode,drugPharmName,commonName,drug,norms,unit,factor,manufacturer){
            $('.modal-title').html('药品修改');
            $('#txtdrugCode').val(drugCode);
            $('#txtdrugPharmCode').val(drugPharmCode);
            $('#txtdrugPharmName').val(drugPharmName);
            $('#txtCommonName').val(commonName);
            $('#txtdrug').val(drug);
            $('#txtnorms').val(norms);
            $('#txtunit').val(unit);
            $('#txtfactor').val(factor);
            $('#txtmanufacturer').val(manufacturer);
             	$('#exampleModal').modal({
                     keyboard: true
                 });
             }
        function save(){
      	  
      	  $.HN.message.confirm('确定要保存吗？', '', '').on(function (e) {
                if (e) {
                    $.HN.message.alert('操作成功！', '提示信息', 'success');
                    window.location.href="drugList.jsp";
                    
                }
            });
      	  
        }
        function checkout(){
        var rowIds = $(".grid_table").jqGrid('getGridParam', 'selarrrow');
        var num1 = rowIds[0];
        var num2 = rowIds[1];
        if(rowIds.length !=2){
        alter("请最多选两个");
        return;
        }
        }

        
        function mergeDrugInfo(){
        	var rowIds = $("#gridlist").jqGrid("getGridParam", "selarrrow") || [];
    		if (rowIds.length <= 0) {
    			$.HN.message.alert("请选择一条要合并的药品！", "消息", "error");
    			return;
    		} else if (rowIds.length > 1){
    			$.HN.message.alert("只能选择一条药品信息合并！", "消息", "error");
    			return;
    		}
    		window.location.href="${pageContext.request.contextPath}/stdDrug/toMergeDrugInfo.html?drugId="+rowIds[0];
        }
        
        
        $("#gridlist2").jqGrid({
            url: "${pageContext.request.contextPath}/dicDrugspec/dicDrugspecList.html",
            mtype: "POST",
            datatype: "json",
            autowidth: true,
            height: 255,
            //shrinkToFit: false,
            colNames: ['id','规格编码','规格名称'],
            colModel: [
     { name: 'specId',hidden:true,key:true, index: 'sdate', width: 90, align: "center"},
     			{ name: 'specCode', index: 'sdate', width: 200, align: "center"},
                { name: 'specName', index: 'sdate', width: 350, align: "left"}
            ],
            rowNum: 10,
            rowList: [10, 20, 30],
            rownumbers: true,
            pager: "#gridpage2",
            //multiselect: true, 
            viewrecords: true,
            caption: "规格选择", 	
            jsonReader: {
                repeatitems: false,
                id: "ids"
            }
        });
        $("#gridlist2").jqGrid('navGrid', '#gridpage2', { add: false, edit: false, del: false, search: false, refresh: false });
        $(window).trigger("resize");
        
        function selectSpec(){
    		$("#specName").val("");
    	    gridSpecSerch();
    	   $('#selectSpec').modal({
    			keyboard : true
    		});
    	   //window.showModalDialog("${pageContext.request.contextPath}/stdDrug/toSelectComp.html","newwin","dialogHeight: 200px; dialogWidth: 150px; dialogTop: 458px; dialogLeft: 166px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;");
      }
      function gridSpecSerch() {
      	var specName = $("#specName").val();
    		$("#gridlist2").jqGrid('setGridParam',{  
    	        datatype:'json',
    	        postData:{"specName":specName}
    	    }).trigger("reloadGrid"); //重新载入  
    	}
      function gridSpecOk(){
    	   var specid = $("#gridlist2").jqGrid('getGridParam', 'selrow');
          if (specid == null) {
       	   $.HN.message.alert('请至少选一行！', '提示信息', 'error');
              return;
          }
          var rowdata = $("#gridlist2").jqGrid('getRowData', specid);
          $("#specId").val(specid);
          $("#drugspec").val(rowdata.specName);
          $("#selectSpec").modal("hide");

      }
      function cleanSpecdata(){
    	   $("#specId").val("");
          $("#drugspec").val("");
          $("#selectSpec").modal("hide");
      }
      
      
      
      
      
      
      $("#gridlist1").jqGrid({
          url: "${pageContext.request.contextPath}/stdCompinfo/stdCompinfoList.html",
          mtype: "POST",
          datatype: "json",
          autowidth: true,
          height: 255,
          shrinkToFit: false,
          colNames: ['id','企业编码','企业名称', '组织机构代码','法人代表'],
          colModel: [
   { name: 'compId',hidden:true,key:true, index: 'sdate', width: 90, align: "center"},
   			{ name: 'compCode', index: 'sdate', width: 90, align: "center"},
              { name: 'compName', index: 'sdate', width: 250, align: "left"},
              { name: 'orgCode', index: 'name', width:100, align: "center" },
              { name: 'lrName', index: 'name', width:80, align: "center" }
          ],
          rowNum: 10,
          rowList: [10, 20, 30],
          rownumbers: true,
          pager: "#gridpage1",
          //multiselect: true, 
          viewrecords: true,
          caption: "企业选择", 	
          jsonReader: {
              repeatitems: false,
              id: "ids"
          }
      });
      $("#gridlist1").jqGrid('navGrid', '#gridpage1', { add: false, edit: false, del: false, search: false, refresh: false });
      $(window).trigger("resize");
      
      function selectComp(){
   	   
   		$("#compCode").val("");
   	    $("#compName").val("");
   	 gridCompSerch();
   	   $('#selectComp').modal({
   			keyboard : true
   		});
   	   
   	  // window.open("${pageContext.request.contextPath}/stdDrug/toSelectComp.html?value=prodPompId&text=comp","newwin","dialogHeight: 200px; dialogWidth: 150px; dialogTop: 458px; dialogLeft: 166px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;");
      }
      function gridCompSerch() {
      	var compCode = $("#compCode").val();
      	var compName = $("#compName").val();
   		$("#gridlist1").jqGrid('setGridParam',{  
   	        datatype:'json',
   	        postData:{"compCode":compCode,
   	        		"compName":compName}
   	    }).trigger("reloadGrid"); //重新载入  
   	}
      function gridOk(){
   	   var compId = $("#gridlist1").jqGrid('getGridParam', 'selrow');
          if (compId == null) {
       	   $.HN.message.alert('请至少选一行！', '提示信息', 'error');
              return;
          }
          var rowdata = $("#gridlist1").jqGrid('getRowData', compId);
          $("#prodPompId").val(compId);
          $("#comp").val(rowdata.compName);
          $("#selectComp").modal("hide");
          
      }
      function cleandata(){
   	   $("#prodPompId").val("");
          $("#comp").val("");
          $("#selectComp").modal("hide");
      }
    </script>
</body>
</html>